﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using System.IO;
using GenerateDiagram;
namespace ToolGenerateDiagram
{
    public partial class DisplayClass : DockContent
    {
        public DisplayClass()
        {
            InitializeComponent();
        }
        private bool _IsSave = false;
        private void DisplayClass_Load(object sender, EventArgs e)
        {
            this.FormClosing += new FormClosingEventHandler(DisplayClass_FormClosing);
        }

        void DisplayClass_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!_IsSave)
            {
                SaveData(true);
               
            }
        }

        public void LoadData(string data)
        {
            this.rtbGenCode.Text = data;
        }

        private void saveFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveData(false);
        }

        public void SaveData(bool question)
        {
            if (question)
            {
                var dia = FormManager.ShowMessageBox("Do you save class "+this.Text+"?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                if (dia == System.Windows.Forms.DialogResult.Yes)
                {
                    if (folderBrowserDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        if (WriteFile(folderBrowserDialog1.SelectedPath))
                            FormManager.ShowMessageBox("Write file completed", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        else
                            FormManager.ShowMessageBox("Write file error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            else
            {
                if (folderBrowserDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    if (WriteFile(folderBrowserDialog1.SelectedPath))
                        FormManager.ShowMessageBox("Write file completed", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    else
                        FormManager.ShowMessageBox("Write file error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            
        }

        public bool WriteFile(string folderPath)
        {
            IOHelper ioHelper = new IOHelper();
            _IsSave = ioHelper.WriteFile(this.rtbGenCode.Text, folderPath + "\\" + this.Text, false);
            return _IsSave;
        }

        private void DisplayClass_FormClosed(object sender, FormClosedEventArgs e)
        {
            MainForm main = this.ParentForm as MainForm;
            main.HandleToolstrip(this.DockPanel.DocumentsCount > 1);
        }
    }
}
